package com.huawei.classroom.student.h19.q05;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

public class StudentUtil {
	public StudentUtil() {
		
	}
	
	public List<String> sort(String fileName) { 
		String all = null;
		try {
			all = readFromTxt(fileName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] big = all.split(";"+"\r\n");
		HashMap<String, Student> mapp = new HashMap<>();
		for(int i=0; i<big.length; i++) {
			String[] small = big[i].split(",");
			if(i == big.length-1) {
				small[2] = small[2].replace(";", "");
			}
			if(!mapp.containsKey(small[0])) {
				Student temp = new Student();
				temp.map.put(small[1], Integer.parseInt(small[2]));
				mapp.put(small[0], temp);
			}
			else {
				mapp.get(small[0]).map.put(small[1], Integer.parseInt(small[2]));
			}
		}
		for(Map.Entry<String, Student> k: mapp.entrySet()) {
			k.getValue().collectAllscore();
		}
		List<Map.Entry<String, Student>> list = new ArrayList<>(mapp.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<String, Student>>() {
			@Override
			public int compare(Map.Entry<String, Student> a1, Map.Entry<String, Student> a2) {
				if(a1.getValue().getAllscore() != a2.getValue().getAllscore()) {
					return a2.getValue().getAllscore() - a1.getValue().getAllscore();
				}
				else if(a1.getValue().map.get("语文") != a2.getValue().map.get("语文")) {
					if(a1.getValue().map.get("语文") == null) {
						return 1;
					}
					else if(a2.getValue().map.get("语文") == null) {
						return -1;
					}
					else {
						return a2.getValue().map.get("语文") - a1.getValue().map.get("语文");
					}
				}
				else if(a1.getValue().map.get("数学") != a2.getValue().map.get("数学")) {
					if(a1.getValue().map.get("数学") == null) {
						return 1;
					}
					else if(a2.getValue().map.get("数学") == null) {
						return -1;
					}
					else {
						return a2.getValue().map.get("数学") - a1.getValue().map.get("数学");
					}
				}
				else {
					if(a1.getValue().map.get("英语") == null) {
						return 1;
					}
					else if(a2.getValue().map.get("英语") == null) {
						return -1;
					}
					else {
						return a2.getValue().map.get("英语") - a1.getValue().map.get("英语");
					}
				}
			}
		});
		
		List<String> fina_list = new ArrayList<>();
		for(Map.Entry<String, Student> i: list) {
			fina_list.add(i.getKey());
		}
		return fina_list;
	}
	
	private String readFromTxt(String filename) throws Exception {
		Reader reader = null;
		try {
			StringBuffer buf = new StringBuffer();
			char[] chars = new char[1024];
			// InputStream in=new FileInputStream(filename);

			reader = new InputStreamReader(new FileInputStream(filename), "UTF-8");
			int readed = reader.read(chars);
			while (readed != -1) {
				buf.append(chars, 0, readed);
				readed = reader.read(chars);
			}
			return buf.toString();
		} finally {
			close(reader);
		}
	}
	
	private void close(Closeable inout) {
		if (inout != null) {
			try {
				inout.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
